package org.lansir.af.ui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.lansir.af.R;
import org.lansir.af.database.DataRowTrusty;
import org.lansir.af.database.DataTable;
import org.lansir.af.dataview.DataViewItem;
import org.lansir.af.dataview.DataViewTrusty;
import org.lansir.af.dataview.DataViewTrustyItem;
import org.lansir.af.gtalk.service.Contact;
import org.lansir.af.gtalk.ui.GroupList;
import org.lansir.af.gtalk.ui.dialogs.builders.Alias;
import org.lansir.af.gtalk.ui.dialogs.builders.DeleteContact;
import org.lansir.af.gtalk.ui.dialogs.builders.ResendSubscription;
import org.lansir.af.view.QuickActionWindow;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class TrustyFragment extends CommonFragment {
	public static final String TAG = "TrustyFragment";
	private static final int REQUESTCODE_MEMBERS = 0x443;

	private ListView lvTrustyList;
	private TrustyAdapter mAdapterTrusty;
	private ImageView ivNoTrustyList;
	
	private List<DataViewTrustyItem> mListTrustyMember;
	private DataViewTrusty dataViewTrusty;
	private DataRowTrusty dataRowTrusty;
	private DataTable dataTableTrusty;
	private LayoutInflater mInflater;
	private List<Contact> mSelectedContacts = new ArrayList<Contact>();
	private Set<DataViewTrustyItem> mSetTrustyMember;
	private QuickActionWindow mQuickActionWindow;
	private DataViewTrustyItem mDeleteContact;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dataRowTrusty = new DataRowTrusty(this.userdb);
		dataTableTrusty = new DataTable(this.dataRowTrusty);
		dataViewTrusty = new DataViewTrusty(this.userdb);
		dataViewTrusty.reloadTable();
		mListTrustyMember = new ArrayList<DataViewTrustyItem>();
		for(DataViewItem dataViewItem :dataViewTrusty.getRowsForTotal()){
			mListTrustyMember.add((DataViewTrustyItem) dataViewItem);
		}
		mSetTrustyMember = new HashSet<DataViewTrustyItem>();
		mSetTrustyMember.addAll(mListTrustyMember);
		
		mQuickActionWindow = new QuickActionWindow(getActivity(), null, null);
		mQuickActionWindow.addItem(getResources().getDrawable(R.drawable.ic_quickaction_delete), getResources().getString(R.string.userinfo_label_delete), new OnClickListener() {
			public void onClick(View v) {
				//mDeleteContact
				
				dataTableTrusty.deleteData(mDeleteContact.getId());
				dataViewTrusty.reloadTable();
				mListTrustyMember.clear();
				mSetTrustyMember.clear();
				for(DataViewItem dataViewItem :dataViewTrusty.getRowsForTotal()){
					mListTrustyMember.add((DataViewTrustyItem) dataViewItem);
				}
				
				mSetTrustyMember.addAll(mListTrustyMember);
				if(mListTrustyMember.size() == 0){
					ivNoTrustyList.setVisibility(View.VISIBLE);
				}
				mAdapterTrusty.notifyDataSetChanged();
				mQuickActionWindow.dismiss();
			}
		});

		
		this.setHasOptionsMenu(true);
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View viewRoot = inflater.inflate(R.layout.fragment_trusty, null);
		mInflater = inflater;
		lvTrustyList = (ListView) viewRoot.findViewById(R.id.lvTrustylist);
		ivNoTrustyList = (ImageView) viewRoot.findViewById(R.id.ivNoTrustyList);
		mAdapterTrusty = new TrustyAdapter();
		lvTrustyList.setAdapter(mAdapterTrusty);
		lvTrustyList.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
				int[] xy = new int[2];
				// fills the array with the computed coordinates
				v.getLocationInWindow(xy);
				// rectangle holding the clicked view area
				Log.v(TAG, "x:" + xy[0] + ",y:" + xy[1] + ",width:" + v.getWidth() + ",height:" + v.getHeight());
				Rect rect = new Rect(xy[0], xy[1], xy[0] + v.getWidth(), xy[1] + v.getHeight());
				mQuickActionWindow.setLocation(v, rect);
				mDeleteContact = (DataViewTrustyItem) mAdapterTrusty.getItem(position);
				mQuickActionWindow.show();
				return false;
			}
			
		});
		if(mListTrustyMember.size() == 0){
			ivNoTrustyList.setVisibility(View.VISIBLE);
		}
		return viewRoot;
	}
	
	
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.schedule_menu_items, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.menu_create:
			startActivityForResult(new Intent("android.intent.action.ACTION_MODE_ACTIVITYCONTACTS"), REQUESTCODE_MEMBERS);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}



	private class TrustyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			
			return mListTrustyMember.size();
		}

		@Override
		public Object getItem(int position) {

			return mListTrustyMember.get(position);
		}

		@Override
		public long getItemId(int position) {

			return mListTrustyMember.get(position).hashCode();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.fragment_schedule_activity_info_participator_listchild, null);
				viewHolder = new ViewHolder();
				viewHolder.tvView = (TextView) convertView.findViewById(R.id.tvActivityInfoParticipatorName);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			viewHolder.tvView.setText(mListTrustyMember.get(position).getName());
			
			return convertView;
		}
		
		private class ViewHolder{
			public TextView tvView;
		}
		
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == Activity.RESULT_OK){
			switch(requestCode){
			case REQUESTCODE_MEMBERS:
				mSelectedContacts = data.getParcelableArrayListExtra(ScheduleContactFragment.RESULT_SELECTED_PARTICIPATOR);
				if(mSelectedContacts.size() != 0)
					ivNoTrustyList.setVisibility(View.GONE);
				for(Contact c : mSelectedContacts){
					DataViewTrustyItem item = new DataViewTrustyItem();
					item.setMemberId(c.getJID().toLowerCase());
					item.setName(c.getName());
					item.setType(DataRowTrusty.DataTrustyTypeTrust);
					
					if(!mSetTrustyMember.contains(item)){
						dataRowTrusty.setName(item.getName());
						dataRowTrusty.setType(item.getType());
						dataRowTrusty.setMemberID(item.getMemberId());
						dataRowTrusty.setValuesForDataRow();
						dataTableTrusty.insertValues();						
						mSetTrustyMember.add(item);
						mListTrustyMember.add(item);
						Log.v(TAG, "add");
					}
				}
				mAdapterTrusty.notifyDataSetChanged();
				
				break;
			}
		}
	
	}
	
}
